package com.example.mychess;

import com.example.mychess.LogonController;
import com.example.mychess.MainController;
import com.example.mychess.Message;
import com.example.mychess.SocketUtil;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;

import static com.example.mychess.LogonController.*;

public class TableController {
    public Button table1;
    public Button table2;
    public Button table3;
    public Button table4;
    public Button table5;
    public Button table6;
    public Button table7;
    public Button table8;
    public Button table9;
    public Button table10;
    public  Text text1;
    public Text text2;
    public Text text3;
    public Text text4;
    public Text text5;
    public Text text6;
    public Text text7;
    public Text text8;
    public Text text9;
    public Text text10;
    public static Stage stg;
    public Text name;
    public Text sex;
    public Text integral;

    public void LogChess(){
        MainController.i=1;
        LogonController.st.hide();
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("main-view.fxml"));
        Scene scene = null;
        try {
            scene = new Scene(fxmlLoader.load(), 450, 500);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        stg = new Stage();
        stg.setTitle("棋盘页面"+ st.getTitle());
        stg.setScene(scene);
        stg.setOnCloseRequest(event -> {
            // 在这里添加你想要在窗口关闭前执行的代码
            System.out.println("执行关闭前的操作...");

            // 如果你想在执行完特定操作后继续关闭窗口，调用event.consume()以防止默认关闭行为
            // 然后手动关闭窗口，例如：
            event.consume();
            mmgg.setType(Message.Type.LEAVE);
            mmgg.setFrom(st.getTitle());
            SocketUtil.send(socket,mmgg);
            st.close();
            stage.close();
            stg.close(); // 结束JavaFX应用程序
            Platform.exit();
        });
        stg.show();
    }
    public void select1(){
        MainController.myTable=1;
        a[0]+=1;
        select(text1);
    }
    public void select2(){
        MainController.myTable=2;
        a[1]+=1;
        select(text2);
    }
    public void select3(){
        MainController.myTable=3;
        a[2]+=1;
        select(text3);
    }
    public void select4(){
        MainController.myTable=4;
        a[3]+=1;
        select(text4);
    }
    public void select5(){
        MainController.myTable=5;
        a[4]+=1;
        select(text5);
    }
    public void select6(){
        MainController.myTable=6;
        a[5]+=1;
        select(text6);
    }
    public void select7(){
        MainController.myTable=7;
        a[6]+=1;
        select(text7);
    }
    public void select8(){
        MainController.myTable=8;
        a[7]+=1;
        select(text8);
    }
    public void select9(){
        MainController.myTable=9;
        a[8]+=1;
        select(text9);
    }
    public void select10(){
        MainController.myTable=10;
        a[9]+=1;
        select(text10);
    }
    public void select(Text text) {
        if(text.getText().equals("0")){
            MainController.yuPlayer=1;
            Message msg=new Message();
            msg.setType(Message.Type.JOIN);
            msg.setTable(1);
            msg.setFrom(LogonController.st.getTitle());
            SocketUtil.send(LogonController.socket,msg);
            Object receive=SocketUtil.receive(socket);
            Message re=(Message) receive;
            System.out.println(re.getContent());
            if(re.getType()==Message.Type.SUCCESS){
                LogChess();
            }
        }else if(text.getText().equals("1")){
            MainController.yuPlayer=0;
            MainController.begin=1;
            Message msg=new Message();
            msg.setType(Message.Type.FIGHT);
            msg.setTable(1);
            msg.setFrom(LogonController.st.getTitle());
            SocketUtil.send(socket,msg);
            Object receive=SocketUtil.receive(socket);
            Message re=(Message) receive;
            System.out.println(re.getContent());
            if(re.getType()==Message.Type.FIGHT){
                LogChess();a[MainController.myTable-1]+=1;
            }
        }else {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示");
            alert.setHeaderText("人数已满");
            alert.show();
        }

    }
    @FXML
    public  void initialize(){
        mmgg.setType(Message.Type.A);
        mmgg.setFrom(myUser.getAccount());
        SocketUtil.send(socket,mmgg);
        mmgg=(Message) SocketUtil.receive(socket);
       ref();
    }

    public static Message mmgg=new Message();
    public void leave(ActionEvent actionEvent) {
        mmgg.setType(Message.Type.LEAVE);
        mmgg.setFrom(st.getTitle());
        SocketUtil.send(socket,mmgg);
        st.close();
        stage.close();
    }
    public void ref(){
        if(mmgg.getType()==Message.Type.A&&mmgg.getA()!=null) {
            a = mmgg.getA();
            text1.setText(a[0] + "");
            text2.setText(a[1] + "");
            text3.setText(a[2] + "");
            text4.setText(a[3] + "");
            text5.setText(a[4] + "");
            text6.setText(a[5] + "");
            text7.setText(a[6] + "");
            text8.setText(a[7] + "");
            text9.setText(a[8] + "");
            text10.setText(a[9] + "");
            name.setText(myUser.getName());
            sex.setText(myUser.getSex());
            integral.setText(myUser.getIntegral()+"");
        }
    }
}
